/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;

import com.mongodb.util.Hash;



//probably deprecated
public class ElasticMapping {
	
	
	private String typeName;
	
	private boolean dynamic = true;
	
	private _id _id;
	private _type _type;
	private _source _source;
	private _all _all;
	private _analyzer _analyzer;
	private _boost _boost; 
	private _parent _parent;
	private _routing _routing;
	private _index _index;
	private _size _size;
	private _timestamp _timestamp;
	private _ttl _ttl;
	 
	
	public static ElasticMapping getFromMappingMetaData(MappingMetaData mappingMetadata ){
		
		try { 
			ElasticMapping elasticMapping = new ElasticMapping();
			byte[] mappingSource = mappingMetadata.source().uncompressed();
	        XContentParser parser = XContentFactory.xContent(mappingSource).createParser(mappingSource);
	        Map<String, Object> base = parser.map();
	        Map.Entry<String, Object> mapping = base.entrySet().iterator().next();
	       
           
        	elasticMapping.typeName = mapping.getKey();
			Object props= mapping.getValue();
			if(props instanceof HashMap){
				HashMap<String, Object> valsMap = (HashMap<String, Object>) props;
				for (Map.Entry<String, Object> prop: valsMap.entrySet()) {
					elasticMapping.putMappingProp(prop.getKey(), prop.getValue());
				}
			}
	        return elasticMapping;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return  null;
		}
	}
	
	
	
	private void putMappingProp(String propKey, Object prop){
		if(prop instanceof String){
			if(propKey.equals("dynamic")){
				try {
					getClass().getField(propKey).set(this, prop);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(prop instanceof HashMap){
			//TODO ??? oO
//			Object o = Class.forName(className)
		
		}

	}
	
	
	
	
	
	private class _property{
		String type; //type of the field = string, float integer etc ..
		String index_name;
		String store;
		String index;
		String term_vector;
		float boost = 1.0F;
		String null_value;
		String omit_norms;
		String omit_term_freq_and_positions;
		String analyzer;
		String index_analyzer;
		String search_analyzer;
		boolean include_in_all = true;
		String precision_step;
		String format;
		
		//nested set of property for "Object" and "Array" indexing
		//(see http://www.elasticsearch.org/guide/reference/mapping/array-type.html   
		//and  http://www.elasticsearch.org/guide/reference/mapping/object-type.html) 
		Set<_property> properties; 
	}
	
	
	private class _id{
		String index;
		String store;
		String path;
	}
	
	private class _type{
		String store;
		String index;
	}
	
	private class _source{
		boolean enabled = true;
		boolean compress = false;
		String compress_threshold;
		Set<String> includes;
		Set<String> excludes;
	}
	
	private class _all{
		boolean enabled = true;
		String store;
		String term_vector;
		String index_analyzer;
		String search_analyzer;
	}
	
	
	private class _analyzer{
		String path;
		String index; //YES / NO
	}
	
	private class _boost{
		String name;
		float null_value; 
	}
	
	private class _parent{
		String type;
	}
	
	private class _routing{
		String store;
		String index;
		boolean required;
		String path;
	}
	
	private class _index{
		boolean enabled;
	}
	
	private class _size{
		boolean enabled;
		String store;
	}
	
	private class _timestamp{
		boolean enabled;
		String store;
		String index;
		String path;
		String dateFormat;
	}
	
	
	private class _ttl{
		boolean enabled;
		String store;
		String index;
		String deflt;
		
	}
	
	
	
	
	
}
